﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKArchUtils.Common;

namespace YJKRevitSteps.CreateFootstep
{
	public class ExtrusionFootstepFamilyCreator : MassingFamilyCreator
	{
		public ExtrusionFootstepFamilyCreator(ExternalCommandData cmdData, ElementId materialId, Autodesk.Revit.DB.Document familyDocument) : base(cmdData, materialId, familyDocument)
		{
		}

		public override Result AddReferencePlanes(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result CreateExtrusion(List<List<LoopEdge>> hierarchicalLoopEdge, double stepHeight, ref string message)
		{
			List<CurveArrArray> list = this.CreateProfileShape(hierarchicalLoopEdge);
			if (list.Count == 0)
			{
				message = "选择的尺寸太小无法生成台阶";
				return Autodesk.Revit.UI.Result.Failed;
			}
			int num = 0;
			foreach (CurveArrArray curveArrArray in list)
			{
				num++;
				SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(this.m_FamilyDocument, this.m_BottomRefPlane.GetRefPlane());
				double num2 = stepHeight * (double)num;
				bool flag = true;
				Extrusion extrusion = null;
				try
				{
					extrusion = this.m_FamilyDocument.FamilyCreate.NewExtrusion(flag, curveArrArray, sketchPlane, num2);
				}
				catch (Exception)
				{
					message = "创建台阶失败，截面无法拉伸";
					return Autodesk.Revit.UI.Result.Failed;
				}
				if (extrusion == null)
				{
					message = "创建台阶失败，截面无法拉伸";
					return Autodesk.Revit.UI.Result.Failed;
				}
				this.m_GenericFormList.Add(extrusion);
			}
			if (this.m_GenericFormList.Count < 1)
			{
				message = "创建台阶失败，截面无法拉伸";
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (this.m_GenericFormList.Count > 1)
			{
				CombinableElementArray combinableElementArray = new CombinableElementArray();
				foreach (GenericForm genericForm in this.m_GenericFormList)
				{
					combinableElementArray.Append(genericForm);
				}
				this.m_FamilyDocument.CombineElements(combinableElementArray);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private List<CurveArrArray> CreateProfileShape(List<List<LoopEdge>> hierarchicalLoopEdge)
		{
			List<CurveArrArray> list = new List<CurveArrArray>();
			foreach (List<LoopEdge> list2 in hierarchicalLoopEdge)
			{
				CurveArrArray curveArrArray = new CurveArrArray();
				CurveArray curveArray = new CurveArray();
				foreach (LoopEdge loopEdge in list2)
				{
					Curve curve = loopEdge.ConvertToCurve(this.m_Revit.Application.Application);
					if (!(curve == null))
					{
						curveArray.Append(curve);
					}
				}
				if (curveArray.Size > 0)
				{
					curveArrArray.Append(curveArray);
				}
				if (curveArrArray.Size > 0)
				{
					list.Add(curveArrArray);
				}
			}
			return list;
		}

		public override Result AddAlignments(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result AddParameters(ref string message)
		{
			FamilyManager familyManager = this.m_FamilyDocument.FamilyManager;
			FamilyParameter familyParameter = familyManager.get_Parameter("材质");
			if (familyParameter == null)
			{
				familyParameter = familyManager.AddParameter("材质", BuiltInParameterGroup.PG_MATERIALS, (Autodesk.Revit.DB.ParameterType)9, false);
			}
			foreach (GenericForm genericForm in this.m_GenericFormList)
			{
				Parameter parameter = ((Extrusion)genericForm).get_Parameter(BuiltInParameter.MATERIAL_ID_PARAM);
				familyManager.AssociateElementParameterToFamilyParameter(parameter, familyParameter);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result AddDimensions(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result AddSymbols(ref string symbolName, ref string message)
		{
			try
			{
				FamilyManager familyManager = this.m_FamilyDocument.FamilyManager;
				symbolName = "type1";
				FamilyType currentType = familyManager.NewType(symbolName);
				familyManager.CurrentType = currentType;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
